from datetime import datetime

from apps.projectbases.models import ProjectPlanHModel, ProjectInfoModel, ProjectLawModel, ProjectCustomerModel
from utils.utils import update_set

def copy_project_base(request, id):
    # 获取原始项目计划头表数据
    original_project_base = ProjectPlanHModel.objects.get(id=id)
    current_datetime = datetime.now()
    new_project_base_data = {
        'serial_number': original_project_base.serial_number,
        'project_name': original_project_base.name,
        'start_date': original_project_base.start_date,
        'end_date': original_project_base.end_date,
        'department': original_project_base.department,
        'standard_curr': original_project_base.standard_curr,
        'project_status': 'O',
        'process_date': current_datetime
    }
    new_head = ProjectPlanHModel.objects.create(**new_project_base_data, **update_set(request, True))

    original_project_info = ProjectInfoModel.objects.filter(project_plan__id=id)
    for info in original_project_info:
        new_project_info_date = {
            'project_plan': new_head,
            'project_type': info.project_type,
            'scope': info.scope,
            'obj_product': info.obj_product,
            'internal_target': info.internal_target,
            'customer_target': info.customer_target,
            'responsibility': info.responsibility
        }
        ProjectPlanHModel.objects.create(**new_project_info_date, **update_set(request, True))

    original_project_law = ProjectLawModel.objects.filter(project_plan__id=id)
    for law in original_project_law:
        new_project_law_data = {
            'project_plan': new_head,
            'legal_require': law.legal_require,
            'safety_require1': law.safety_require1,
            'safety_require2': law.safety_require2,
            'external_service': law.external_service,
            'delivery_modes': law.delivery_modes,
            'manage_level': law.manage_level,
            'achievement': law.achievement,
            'quality_goal': law.quality_goal,
            'inspection': law.inspection,
            'internal_process': law.internal_process,
            'customer_process': law.customer_process
        }
        ProjectLawModel.objects.create(**new_project_law_data, **update_set(request, True))

    original_project_custom = ProjectCustomerModel.objects.filter(project_plan__id=id)
    for customer in original_project_custom:
        new_project_customer_data = {
            'project_plan': new_head,
            'customer_code': customer.customer_code,
            'customer_name': customer.customer_name,
            'customer_url': customer.customer_url,
            'customer_product': customer.customer_product,
            'product_form': customer.product_form,
            'factory_num': customer.factory_num,
            'stock_clusters_num': customer.stock_clusters_num,
            'address': customer.address,
            'product_num': customer.product_num,
            'item_num': customer.item_num
        }
        ProjectCustomerModel.objects.create(**new_project_customer_data, **update_set(request, True))
